<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-upload
        class="avatar-uploader"
        action="http://192.168.12.75:3009/api/v1/common/file_upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="coverImg" :src="coverImg" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { saveData } from '../../api/products'
import { post } from '../../utils/request'
export default {
  data() {
    return {
      // 表单中的数据
      ruleForm: {
        name: '',
        price: 0,
        coverImg: ''
      },
      coverImg: '',
      // 验证规则
      rules: {
        name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      // $refs属性可以获取当前的dom元素
      //  调用表单的验证方法validate
      //  如果成功返回一个true
      this.$refs[formName].validate((valid) => {
        // eslint-disable-line
        if (valid) {
          // alert('submit!');
          // 此处做数据提交
          saveData(this.ruleForm).then(() => {
            this.$router.push({
              name: 'ProductList'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      post('/api/v1/admin/products', this.ruleForm).then((res) => {
        console.log(res)
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleAvatarSuccess(res, file) {
      this.coverImg = URL.createObjectURL(file.raw)
      console.log(res)
      this.ruleForm.coverImg = res.info
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
